package cn.juque.jdfsystem.listener;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.juque.common.utils.FastJsonUtil;
import cn.juque.jdfsystem.entity.FileInfo;
import cn.juque.jdfsystem.threads.DownloadFileThreadPool;
import cn.juque.lucenecandy.core.datasync.IDataSyncService;
import cn.juque.lucenecandy.core.datasync.listener.ITccControllerListener;
import cn.juque.lucenecandy.vo.IndexUpdateParamVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>其他节点文件信息同步到本地后，下载文件</li>
 * </ul>
 * @date 2024-02-14
 **/
@Component("fileAddTccControllerListener")
public class FileAddTccControllerListener implements ITccControllerListener {

    @Resource
    private DownloadFileThreadPool downloadFileThreadPool;

    @Override
    public Boolean before(IDataSyncService.SyncTypeEnum syncTypeEnum, IndexUpdateParamVO param) {
        return true;
    }

    @Override
    public void after(IDataSyncService.SyncTypeEnum syncTypeEnum, IndexUpdateParamVO param) {
        if (!ClassUtil.equals(FileInfo.class, param.getClassName(), false)) {
            return;
        }
        if (!IDataSyncService.SyncTypeEnum.COMMIT.equals(syncTypeEnum)) {
            return;
        }
        List<FileInfo> list = FastJsonUtil.parseArray(param.getContentStr(), FileInfo.class);
        if (CollUtil.isEmpty(list)) {
            return;
        }
        list.forEach(f -> this.downloadFileThreadPool.execute(null, f));
    }
}
